//
//  PropertyViewController.m
//  TriumphInTheSkiesAPP
//
//  Created by 要帅气的生活 on 15/11/3.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "PropertyViewController.h"
#import "MJRefresh.h"
#import "NetWorkEngine.h"
#import "Size.h"
#import "ToolUtil.h"
#import "AreaViewController.h"
#import "ScreeningViewController.h"
#import "PropertyTableViewCell.h"
#import "PropertyModel.h"
#import "PropertyDetailViewController.h"
#import "LunBoTuViewController.h"
@interface PropertyViewController ()

@property (nonatomic, strong)ToolUtil * toolUtilProvinces;
@property (nonatomic, strong)ToolUtil * toolUtilCity;

@property (nonatomic, strong)NSMutableArray * provincesArray;
@property (nonatomic, strong)NSMutableArray * citysArray;

@end

@implementation PropertyViewController
- (void)dealloc
{
    [self.engine stopLoading];
    
}
static int j = 1;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self confirmChooseView];
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadNewData];
//    self.fromControl = self;
//    self.view.backgroundColor = [UIColor blueColor];
//    self.navigationController.navigationBar.hidden = NO;
//    [self createTitle:@"" midTitle:@"楼盘"];
    
    [self.tableView registerClass:[PropertyTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.tableView.footer.hidden = YES;
    
    
}

- (void)confirmChooseView
{
    
    self.navigationItem.title = @"楼盘";
//    self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];
//    [self.navigationController.navigationBar setTitleTextAttributes:
//     
//   @{NSFontAttributeName:[UIFont systemFontOfSize:17],
//    
//    NSForegroundColorAttributeName:[UIColor whiteColor]}];
//    NSArray * array = [NSArray arrayWithObjects:@"地区",@"筛选",@"排序", nil];
    
    [ToolUtil createVerticalLine:margin_64 superView:self.view lift:WindowWidth/2 height:margin_36];
    [ToolUtil createLine:margin_100 superView:self.view lift:0 width:WindowWidth hight:0.5 color:[UIColor grayColor
                                                                                                  ]];
    self.provincesArray = [NSMutableArray arrayWithObjects:@"浙江", @"广东",@"海南",@"江苏",@"上海",@"北京",@"福建",@"广西",@"湖南",@"湖北",nil];
    self.citysArray = [NSMutableArray arrayWithObjects:@"杭州",@"广州",@"海口",@"宁波",@"南京",@"苏州", nil];
    
    self.toolUtilProvinces = [[ToolUtil alloc] init];
    self.toolUtilProvinces.commboxArray = [NSMutableArray arrayWithArray:@[@"选择省份"]];
    for (int i = 0; i < self.provincesArray.count; i++) {
        [self.toolUtilProvinces.commboxArray addObject:self.provincesArray[i]];
    }
    
    self.toolUtilCity = [[ToolUtil alloc] init];
    self.toolUtilCity.commboxArray = [NSMutableArray arrayWithArray:@[@"选择城市"]];
    for (int i = 0; i < self.citysArray.count; i++) {
        [self.toolUtilCity.commboxArray addObject:self.citysArray[i]];
    }
    
    float txt_h = title_height * 0.9, txt_w = WindowWidth / 2 - 0.5;
    CGRect frame = CGRectMake(0, margin_64, txt_w, txt_h);
    
    float line_y = CGRectGetMaxY(frame), panel_h = WindowHeight - line_y - 6;
    CGRect panelFrame = CGRectMake(2, line_y, txt_w - 4, panel_h);
    [self.toolUtilProvinces createCommbox:@"" _rect:frame maxLength:0 control:self view:self.view callback:^(NSMutableDictionary * dic) {
//        if (j == 1) {
//            
//            self.toolUtilProvinces.commbox.text = self.toolUtilProvinces.commboxArray[0];
//        }else{
//            
//        }
        
        
    } text:@"选择省份" panelFrame:panelFrame isInput:NO align:NSTextAlignmentRight];
    
    float txt_h1 = title_height * 0.9, txt_w1 = WindowWidth / 2 - 0.5;
    CGRect frame1 = CGRectMake(txt_w1 + 0.5, margin_64, txt_w1, txt_h1);
    
    float line_y1 = CGRectGetMaxY(frame), panel_h1 = WindowHeight - line_y1 - 6;
    CGRect panelFrame1 = CGRectMake(txt_w1 + 0.5, line_y1, txt_w1 - 4, panel_h1);
    [self.toolUtilCity createCommbox:@"" _rect:frame1 maxLength:0 control:self view:self.view callback:^(NSMutableDictionary * dic) {
//        if (j == 2) {
//            
//            self.toolUtilCity.commbox.text = self.toolUtilCity.commboxArray[0];
//        }else{
//            
//        }
        
        
    } text:@"选择城市" panelFrame:panelFrame1 isInput:NO align:NSTextAlignmentRight];
    

}
- (void)creatLine:(CGFloat)x y:(CGFloat)y width:(CGFloat)width height:(CGFloat)height
{
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    line.backgroundColor = [UIColor grayColor];
    [self.view addSubview:line];
    
}
- (void)doClick:(UIButton *)btn
{
    if (btn.tag == 100) {
//        self.area = [[AreaViewController alloc] init];
//        [self jump:@"" to:[[AreaViewController alloc] init]];
        NSLog(@"1");
    }else if (btn.tag == 101){
        ScreeningViewController * screenVC = [[ScreeningViewController alloc] init];
        [self.navigationController pushViewController:screenVC animated:YES];
        NSLog(@"2");
    }else if (btn.tag == 102){
        NSLog(@"3");
    }
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 105, WindowWidth, WindowHeight - 165 )];
        _tableView.rowHeight = margin_110;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (void)loadNewData
{
    self.pageNumber = 1;
    self.isPullRefresh = YES;
    [self loadDataWithPageNumber:self.pageNumber];
}
- (void)loadMoreData
{
    self.isPullRefresh = NO;
    [self loadDataWithPageNumber:++self.pageNumber];
    
}

- (void)loadDataWithPageNumber:(NSInteger)pageNum
{
    self.engine = [[NetWorkEngine alloc] init];
    NSString * urlString = [NSString stringWithFormat:@"jj"];
    [_engine get:urlString];
    _engine.delegate = self;
    
    
    
}

- (void)NetWorkDidSuccessful:(NSDictionary *)dic
{
    if (self.isPullRefresh) {
//        [self.dataArray removeAllObjects];
    }
    NSArray * contentArray = dic[@"kak"];
    for (NSDictionary * newsDic in contentArray) {
        PropertyModel * propertyModel = [[PropertyModel alloc] initWithDictionary:newsDic];
//        [self.dataArray addObject:propertyModel];
    }
    [self.tableView reloadData];
    if (contentArray.count < 20) {
        self.tableView.footer.hidden = YES;
    }else{
        self.tableView.footer.hidden = NO;
    }
    [self.tableView.header endRefreshing];
    [self.tableView.footer endRefreshing];

}
- (void)NetWorkDidFail:(NSError *)error
{
    [self.tableView.header endRefreshing];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"cell";
    PropertyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.aImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"shouye%ld.jpg",indexPath.row + 1]];
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//    }
//    cell.selectionStyle = UITableViewCellSelectionStyleGray;
//    cell.textLabel.text = @"jkhjkhljl";
//    PropertyModel * model = [self.dataArray objectAtIndex:indexPath.row];
//    cell.propertyModel = model;
    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [self jump:@"" to:];
    [self.navigationController pushViewController:[[LunBoTuViewController alloc] init] animated:YES];
    self.tabBarController.tabBar.hidden = YES;
//    [self.navigationController pushViewController:[[LunBoTuViewController alloc] init] animated:NO];
    
}
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
